﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using FlexCell;
using YArchitech.Controls;
using YArchitech.ProgressBar;
using YArchitech.Revit;
using HyMaterialStatistic;

namespace PlumbingMaterialStatistic
{
	internal class PlumbingEquipMaterialList : MEPSetDataToGridParent
	{
		public PlumbingEquipMaterialList(Autodesk.Revit.DB.Document doc, List<Dictionary<int, SatatisticDataInfo>> lstDicElement, string gridName, GridSetInfo gridSetInfo, ref FlexCell.Grid grid)
		{
			this.m_doc = doc;
			this.m_grid = grid;
			this.m_lstDicElems = lstDicElement;
			this.m_strGridName = gridName;
			this.m_gridSetInfo = gridSetInfo;
		}

		public bool GetStatisticGrid()
		{
			if (this.m_lstDicElems.Count <= 0 || this.m_gridSetInfo == null)
			{
				return false;
			}
			this.m_grid.Locked = false;
			this.GetEquipmentData();
			this.GetMaterialData();
			if (!this.m_gridSetInfo.TitleDown)
			{
				if (!this.SetDataToGridUp())
				{
					return false;
				}
			}
			else if (!this.SetDataToGridDown())
			{
				return false;
			}
			return true;
		}

		private void GetEquipmentData()
		{
			if (this.m_doc == null)
			{
				return;
			}
			ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstDicElems.Count, "正在初始化设备材料表中的数据", ProgressBarStyle.Blocks, true, false, false);
			progressBarForm.Show(WindowHelper.HWndRevit);
			int num = 1;
			foreach (Dictionary<int, SatatisticDataInfo> dictionary in this.m_lstDicElems)
			{
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					break;
				}
				if (dictionary.Count > 0)
				{
					Dictionary<int, List<string>> dictionary2 = new Dictionary<int, List<string>>();
					foreach (KeyValuePair<int, SatatisticDataInfo> keyValuePair in dictionary)
					{
						ElementId elementId = new ElementId(keyValuePair.Key);
						Element element = this.m_doc.GetElement(elementId);
						int key = -1;
						if (this.EquipElemExsit(keyValuePair.Value.Name, keyValuePair.Value.ElemParm, dictionary2, out key))
						{
							string text = (Convert.ToDouble(dictionary2[key][5]) + keyValuePair.Value.Amount).ToString("0.00");
							if (text.IndexOf('.') != -1)
							{
								text = text.TrimEnd(new char[]
								{
									'0'
								});
								text = text.Trim(new char[]
								{
									'.'
								});
							}
							dictionary2[key][5] = text.ToString();
						}
						else
						{
							List<string> list = new List<string>();
							list.Insert(0, keyValuePair.Value.FilterType);
							list.Insert(1, num.ToString());
							list.Insert(2, keyValuePair.Value.Name);
							list.Insert(3, keyValuePair.Value.ElemParm);
							string item = "";
							try
							{
								if (element is FamilyInstance)
								{
									item = (element as FamilyInstance).Symbol.GetParameter("材质").AsValueString();
								}
								else
								{
									item = element.GetParameter(BuiltInParameter.RBS_PIPE_MATERIAL_PARAM).AsValueString();
								}
							}
							catch
							{
								item = "";
							}
							list.Insert(4, item);
							string text2 = keyValuePair.Value.Amount.ToString("0.00");
							if (text2.IndexOf('.') != -1)
							{
								text2 = text2.TrimEnd(new char[]
								{
									'0'
								});
								text2 = text2.Trim(new char[]
								{
									'.'
								});
							}
							list.Insert(5, text2);
							list.Insert(6, "");
							list.Insert(7, "");
							list.Insert(8, "");
							dictionary2.Add(num, list);
							num++;
						}
					}
					this.m_lstDicEquipData.Add(dictionary2);
				}
			}
		}

		private void GetMaterialData()
		{
			if (this.m_doc == null)
			{
				return;
			}
			int index;
			if (this.m_gridSetInfo.Legend)
			{
				index = 6;
			}
			else
			{
				index = 5;
			}
			ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstDicElems.Count, "正在初始化设备材料表中的数据", ProgressBarStyle.Blocks, true, false, false);
			progressBarForm.Show(WindowHelper.HWndRevit);
			int num = 1;
			foreach (Dictionary<int, SatatisticDataInfo> dictionary in this.m_lstDicElems)
			{
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					break;
				}
				if (dictionary.Count > 0)
				{
					Dictionary<int, List<string>> dictionary2 = new Dictionary<int, List<string>>();
					foreach (KeyValuePair<int, SatatisticDataInfo> keyValuePair in dictionary)
					{
						ElementId elementId = new ElementId(keyValuePair.Key);
						Element element = this.m_doc.GetElement(elementId);
						string text;
						if (element is FamilyInstance)
						{
							text = (element as FamilyInstance).Symbol.Family.Name;
						}
						else
						{
							text = element.GetParameter(BuiltInParameter.ELEM_FAMILY_PARAM).AsValueString() + "_" + element.Name;
						}
						int key = -1;
						if (this.MaterialElemExsit(text, keyValuePair.Value.ElemParm, dictionary2, out key))
						{
							string text2 = (Convert.ToDouble(dictionary2[key][index]) + keyValuePair.Value.Amount).ToString("0.00");
							if (text2.IndexOf('.') != -1)
							{
								text2 = text2.TrimEnd(new char[]
								{
									'0'
								});
								text2 = text2.Trim(new char[]
								{
									'.'
								});
							}
							dictionary2[key][index] = text2.ToString();
						}
						else
						{
							List<string> list = new List<string>();
							int num2 = 0;
							list.Insert(num2, keyValuePair.Value.FilterType);
							list.Insert(++num2, num.ToString());
							if (this.m_gridSetInfo.Legend)
							{
								list.Insert(++num2, keyValuePair.Value.FamilyName);
							}
							list.Insert(++num2, text);
							list.Insert(++num2, keyValuePair.Value.ElemParm);
							list.Insert(++num2, keyValuePair.Value.Unit);
							string text3 = keyValuePair.Value.Amount.ToString("0.00");
							if (text3.IndexOf('.') != -1)
							{
								text3 = text3.TrimEnd(new char[]
								{
									'0'
								});
								text3 = text3.Trim(new char[]
								{
									'.'
								});
							}
							list.Insert(++num2, text3);
							list.Insert(num2 + 1, "");
							dictionary2.Add(num, list);
							num++;
						}
					}
					this.m_lstDicMaterialData.Add(dictionary2);
				}
			}
		}

		private bool EquipElemExsit(string name, string elemParm, Dictionary<int, List<string>> dicEquipData, out int key)
		{
			foreach (int num in dicEquipData.Keys)
			{
				List<string> list = dicEquipData[num];
				if (name == list[2] && elemParm == list[3])
				{
					key = num;
					return true;
				}
			}
			key = -1;
			return false;
		}

		private bool MaterialElemExsit(string name, string elemParm, Dictionary<int, List<string>> dicMaterialData, out int key)
		{
			int index;
			int index2;
			if (this.m_gridSetInfo.Legend)
			{
				index = 3;
				index2 = 4;
			}
			else
			{
				index = 2;
				index2 = 3;
			}
			foreach (int num in dicMaterialData.Keys)
			{
				List<string> list = dicMaterialData[num];
				if (name == list[index] && elemParm == list[index2])
				{
					key = num;
					return true;
				}
			}
			key = -1;
			return false;
		}

		private bool SetDataToGridUp()
		{
			if (this.m_lstDicEquipData.Count <= 0 || this.m_lstDicMaterialData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有统计数据");
				return false;
			}
			this.m_grid.Cell(1, 1).Text = this.m_strGridName;
			this.m_grid.Cell(1, 1).FontSize = this.m_gridSetInfo.TitleFontSize;
			int num = 4;
			this.SetEquipDataToGridUp(ref num);
			num++;
			this.SetMaterialDataToGridUp(num);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontName = this.m_gridSetInfo.FontName;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontSize = this.m_gridSetInfo.FontSize;
			for (int i = 1; i < this.m_grid.Rows; i++)
			{
				this.m_grid.Row(i).AutoFit();
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

		private void SetEquipDataToGridUp(ref int nStartRow)
		{
			int num = 0;
			foreach (Dictionary<int, List<string>> dictionary in this.m_lstDicEquipData)
			{
				num += dictionary.Count;
			}
			if (num - 3 > 0)
			{
				this.m_grid.InsertRow(nStartRow, num - 3);
			}
			else
			{
				this.m_grid.Range(nStartRow + num, 1, nStartRow + 2, 8).DeleteByRow();
			}
			int num2 = 1;
			foreach (Dictionary<int, List<string>> dictionary2 in this.m_lstDicEquipData)
			{
				List<List<int>> list = base.GroupingDicData(2, dictionary2);
				for (int i = 0; i < list.Count; i++)
				{
					List<int> list2 = list[i];
					base.SortData(3, dictionary2, ref list2);
					foreach (int key in list2)
					{
						List<string> list3 = dictionary2[key];
						if (list3.Count > 0)
						{
							for (int j = 1; j < list3.Count; j++)
							{
								if (j == 1)
								{
									this.m_grid.Cell(nStartRow, j).Text = num2.ToString();
								}
								else if (j == 5)
								{
									this.m_grid.Cell(nStartRow, j).Text = Math.Ceiling(Convert.ToDouble(list3[j]) * (1.0 + (double)this.m_gridSetInfo.Margin / 100.0)).ToString("0");
								}
								else
								{
									this.m_grid.Cell(nStartRow, j).Text = list3[j];
								}
								this.m_grid.Cell(nStartRow, j).Alignment = AlignmentEnum.CenterCenter;
							}
							nStartRow++;
							num2++;
						}
					}
				}
			}
		}

		private void SetMaterialDataToGridUp(int nStartRow)
		{
			int num = 0;
			foreach (Dictionary<int, List<string>> dictionary in this.m_lstDicMaterialData)
			{
				num += dictionary.Count;
			}
			int firstCol = 0;
			int num2 = 0;
			int nNameIndex = 0;
			int nSepcIndex = 0;
			int num3 = -1;
			if (this.m_gridSetInfo.Legend)
			{
				firstCol = 7;
				num2 = 6;
				nNameIndex = 3;
				nSepcIndex = 4;
				num3 = 2;
			}
			else
			{
				firstCol = 6;
				num2 = 5;
				nNameIndex = 2;
				nSepcIndex = 3;
			}
			if (num - 3 > 0)
			{
				this.m_grid.InsertRow(nStartRow, num - 3);
			}
			else
			{
				this.m_grid.Range(nStartRow + num, 1, nStartRow + 2, 8).DeleteByRow();
			}
			int num4 = 1;
			foreach (Dictionary<int, List<string>> dictionary2 in this.m_lstDicMaterialData)
			{
				List<List<int>> list = base.GroupingDicData(nNameIndex, dictionary2);
				for (int i = 0; i < list.Count; i++)
				{
					List<int> list2 = list[i];
					base.SortData(nSepcIndex, dictionary2, ref list2);
					foreach (int key in list2)
					{
						List<string> list3 = dictionary2[key];
						if (list3.Count > 0)
						{
							int j = 1;
							while (j < list3.Count)
							{
								if (j == 1)
								{
									this.m_grid.Cell(nStartRow, j).Text = num4.ToString();
									goto IL_22A;
								}
								if (j == num3)
								{
									string text = list3[j];
									string legendByFamilyName = FamilyLegend.GetLegendByFamilyName(this.m_doc, text);
									text = Path.GetFileName(legendByFamilyName);
									try
									{
										this.m_grid.Images.Add(legendByFamilyName, text);
										if (this.m_grid.Images.Item(text) == null)
										{
											goto IL_23F;
										}
										this.m_grid.Cell(nStartRow, j).SetImage(text);
										goto IL_22A;
									}
									catch
									{
										goto IL_23F;
									}
								}
								if (j == num2)
								{
									this.m_grid.Cell(nStartRow, j).Text = Math.Ceiling(Convert.ToDouble(list3[j]) * (1.0 + (double)this.m_gridSetInfo.Margin / 100.0)).ToString("0");
									goto IL_22A;
								}
								this.m_grid.Cell(nStartRow, j).Text = list3[j];
								goto IL_22A;
								IL_23F:
								j++;
								continue;
								IL_22A:
								this.m_grid.Cell(nStartRow, j).Alignment = AlignmentEnum.CenterCenter;
								goto IL_23F;
							}
							this.m_grid.Range(nStartRow, firstCol, nStartRow, 8).Merge();
							nStartRow++;
							num4++;
						}
					}
				}
			}
		}

		private bool SetDataToGridDown()
		{
			if (this.m_lstDicEquipData.Count <= 0 || this.m_lstDicMaterialData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有统计数据");
				return false;
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).DeleteByRow();
			int num = 1;
			foreach (Dictionary<int, List<string>> dictionary in this.m_lstDicEquipData)
			{
				num += dictionary.Count;
			}
			foreach (Dictionary<int, List<string>> dictionary2 in this.m_lstDicMaterialData)
			{
				num += dictionary2.Count;
			}
			this.m_grid.Rows = num + 4;
			this.m_grid.Cols = 9;
			this.DrawEquipGridTitle(num);
			int num2 = 0;
			foreach (Dictionary<int, List<string>> dictionary3 in this.m_lstDicMaterialData)
			{
				num2 += dictionary3.Count;
			}
			this.DrawMaterialGridTitle(num2);
			this.SetEquipDataToGridDown(num);
			this.SetMaterialDataToGridDown(num2);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			this.m_grid.Range(1, 1, num + 2, this.m_grid.Cols - 1).FontName = this.m_gridSetInfo.FontName;
			this.m_grid.Range(1, 1, num + 2, this.m_grid.Cols - 1).FontSize = this.m_gridSetInfo.FontSize;
			for (int i = 1; i < this.m_grid.Rows; i++)
			{
				this.m_grid.Row(i).AutoFit();
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

		private void SetEquipDataToGridDown(int nStartRow)
		{
			int num = 1;
			foreach (Dictionary<int, List<string>> dictionary in this.m_lstDicEquipData)
			{
				List<List<int>> list = base.GroupingDicData(2, dictionary);
				for (int i = 0; i < list.Count; i++)
				{
					List<int> list2 = list[i];
					base.SortData(3, dictionary, ref list2);
					foreach (int key in list2)
					{
						List<string> list3 = dictionary[key];
						if (list3.Count > 0)
						{
							for (int j = 1; j < list3.Count; j++)
							{
								if (j == 1)
								{
									this.m_grid.Cell(nStartRow, j).Text = num.ToString();
								}
								else if (j == 5)
								{
									this.m_grid.Cell(nStartRow, j).Text = Math.Ceiling(Convert.ToDouble(list3[j]) * (1.0 + (double)this.m_gridSetInfo.Margin / 100.0)).ToString("0");
								}
								else
								{
									this.m_grid.Cell(nStartRow, j).Text = list3[j];
								}
								this.m_grid.Cell(nStartRow, j).Alignment = AlignmentEnum.CenterCenter;
							}
							nStartRow--;
							num++;
						}
					}
				}
			}
		}

		private void SetMaterialDataToGridDown(int nStartRow)
		{
			int firstCol = 0;
			int num = 0;
			int nNameIndex = 0;
			int nSepcIndex = 0;
			int num2 = -1;
			if (this.m_gridSetInfo.Legend)
			{
				firstCol = 7;
				num = 6;
				nNameIndex = 3;
				nSepcIndex = 4;
				num2 = 2;
			}
			else
			{
				firstCol = 6;
				num = 5;
				nNameIndex = 2;
				nSepcIndex = 3;
			}
			int num3 = 1;
			foreach (Dictionary<int, List<string>> dictionary in this.m_lstDicMaterialData)
			{
				List<List<int>> list = base.GroupingDicData(nNameIndex, dictionary);
				for (int i = 0; i < list.Count; i++)
				{
					List<int> list2 = list[i];
					base.SortData(nSepcIndex, dictionary, ref list2);
					foreach (int key in list2)
					{
						List<string> list3 = dictionary[key];
						if (list3.Count > 0)
						{
							int j = 1;
							while (j < list3.Count)
							{
								if (j == 1)
								{
									this.m_grid.Cell(nStartRow, j).Text = num3.ToString();
									goto IL_1BA;
								}
								if (j == num2)
								{
									string text = list3[j];
									string legendByFamilyName = FamilyLegend.GetLegendByFamilyName(this.m_doc, text);
									text = Path.GetFileName(legendByFamilyName);
									try
									{
										this.m_grid.Images.Add(legendByFamilyName, text);
										if (this.m_grid.Images.Item(text) == null)
										{
											goto IL_1CF;
										}
										this.m_grid.Cell(nStartRow, j).SetImage(text);
										goto IL_1BA;
									}
									catch
									{
										goto IL_1CF;
									}
								}
								if (j == num)
								{
									this.m_grid.Cell(nStartRow, j).Text = Math.Ceiling(Convert.ToDouble(list3[j]) * (1.0 + (double)this.m_gridSetInfo.Margin / 100.0)).ToString("0");
									goto IL_1BA;
								}
								this.m_grid.Cell(nStartRow, j).Text = list3[j];
								goto IL_1BA;
								IL_1CF:
								j++;
								continue;
								IL_1BA:
								this.m_grid.Cell(nStartRow, j).Alignment = AlignmentEnum.CenterCenter;
								goto IL_1CF;
							}
							this.m_grid.Range(nStartRow, firstCol, nStartRow, 8).Merge();
							nStartRow--;
							num3++;
						}
					}
				}
			}
		}

		private void DrawEquipGridTitle(int nRows)
		{
			this.m_grid.Cell(nRows + 1, 1).Text = "序\r号";
			this.m_grid.Range(nRows + 1, 1, nRows + 2, 1).Merge();
			this.m_grid.Cell(nRows + 2, 2).Text = "名称";
			this.m_grid.Range(nRows + 1, 2, nRows + 2, 2).Merge();
			this.m_grid.Cell(nRows + 2, 3).Text = "型号（规格）";
			this.m_grid.Range(nRows + 1, 3, nRows + 2, 3).Merge();
			this.m_grid.Cell(nRows + 2, 4).Text = "材料";
			this.m_grid.Range(nRows + 1, 4, nRows + 2, 4).Merge();
			this.m_grid.Cell(nRows + 2, 5).Text = "件\r数";
			this.m_grid.Range(nRows + 1, 5, nRows + 2, 5).Merge();
			this.m_grid.Cell(nRows + 2, 6).Text = "重量";
			this.m_grid.Range(nRows + 2, 6, nRows + 2, 7).Merge();
			this.m_grid.Cell(nRows + 1, 6).Text = "单件";
			this.m_grid.Cell(nRows + 1, 7).Text = "合计";
			this.m_grid.Cell(nRows + 2, 8).Text = "备注";
			this.m_grid.Range(nRows + 1, 8, nRows + 2, 8).Merge();
			this.m_grid.Cell(nRows + 3, 1).Text = this.m_strGridName;
			this.m_grid.Range(nRows + 3, 1, nRows + 3, 8).Merge();
			this.m_grid.Cell(nRows + 3, 1).FontSize = this.m_gridSetInfo.TitleFontSize;
			this.m_grid.Range(nRows + 1, 1, nRows + 3, 8).Alignment = AlignmentEnum.CenterCenter;
		}

		private void DrawMaterialGridTitle(int nRows)
		{
			int num = 1;
			this.m_grid.Cell(nRows + 1, num).Text = "序\r号";
			if (this.m_gridSetInfo.Legend)
			{
				this.m_grid.Cell(nRows + 1, ++num).Text = "图例";
			}
			this.m_grid.Cell(nRows + 1, ++num).Text = "名称";
			this.m_grid.Cell(nRows + 1, ++num).Text = "规格";
			this.m_grid.Cell(nRows + 1, ++num).Text = "单位";
			this.m_grid.Cell(nRows + 1, ++num).Text = "数量";
			this.m_grid.Cell(nRows + 1, ++num).Text = "备注";
			this.m_grid.Range(nRows + 1, num, nRows + 1, 8).Merge();
			this.m_grid.Range(nRows + 1, 1, nRows + 1, 8).Alignment = AlignmentEnum.CenterCenter;
		}

		private Document m_doc;

		private List<Dictionary<int, SatatisticDataInfo>> m_lstDicElems;

		private FlexCell.Grid m_grid;

		private List<Dictionary<int, List<string>>> m_lstDicEquipData = new List<Dictionary<int, List<string>>>();

		private List<Dictionary<int, List<string>>> m_lstDicMaterialData = new List<Dictionary<int, List<string>>>();

		private string m_strGridName = "";

		private GridSetInfo m_gridSetInfo;
	}
}
